/*
 * FileComparisor.h
 *
 *  Created on: Aug 20, 2013
 *      Author: jeff
 */

#ifndef FILECOMPARISOR_H_
#define FILECOMPARISOR_H_

#include "Comparisor.h"

#include <fstream>
#include <iostream>

#define POST_FIX		".seg"
#define JSON_KEY_BLOCK	"blocks"
#define JSON_KEY_RIGHT_FILE			"file"
#define JSON_KEY_SEGMENT_FILE			"segment_file"

#define BUFFER_SIZE		(1024*1024*64)
namespace fs {

class RawFileComparisor: public Comparisor {
private:
	std::string right_filename;
	std::string segment_filename;

protected:
	Json::Value root;

	std::ofstream segment_file;
	std::fstream fs_left;
	std::fstream fs_right;
	std::filebuf *cursor_left;
	std::filebuf *cursor_right;

	std::fstream fs_out;

	void close_segment_file();
	void open_segment_file();

	virtual void init_left_input(std::string left);
	virtual void init_right_input(std::string right);
	virtual int read_left(char * buffer, int length);
	virtual int read_right(char * buffer, int length);
	virtual void close_left_input();
	virtual void close_right_input();

	virtual std::string get_segment_filename();

	void output_diff_data(int insert_length, const char * diff, Json::Value* segment);
public:
	int buffer_size;

	RawFileComparisor():
		right_filename(""),
		segment_filename(""),
		fs_left(0),
		fs_right(0),
		cursor_left(NULL),
		cursor_right(NULL),
		buffer_size(BUFFER_SIZE){};
	virtual ~RawFileComparisor(){};

	void setLeft(std::string left);
	void setRight(std::string right);
	Json::Value compare();
};

}
#endif /* FILECOMPARISOR_H_ */
